package com.unionftech.common.recaptcha

import android.app.Activity
import com.netease.nis.captcha.Captcha
import com.netease.nis.captcha.CaptchaConfiguration
import com.netease.nis.captcha.CaptchaConfiguration.LangType
import com.netease.nis.captcha.CaptchaListener
import com.unionftech.common.CommonConstants
import com.unionftech.common.utils.LogUtils
import com.unionftech.common.utils.SpUtils

class YiDunCaptchaUtil private constructor() {

    companion object {
        private const val TAG = "YiDunCaptchaUtil"
        private const val CAPTCHA_ID = CommonConstants.Config.NETEASE_CAPTCHA_ID

        // yidun 语言映射
        private val yidunLocaleMapping = mutableMapOf<String, LangType>(
            "zh-CN" to LangType.LANG_ZH_CN,
            "zh-TW" to LangType.LANG_ZH_TW,
            "en" to LangType.LANG_EN_US,
            "vn" to LangType.LANG_VI,
            "th" to LangType.LANG_TH,
            "my" to LangType.LANG_MS,
            "ko" to LangType.LANG_KO,
            "ja" to LangType.LANG_JA,
            "fr" to LangType.LANG_FR,
            "pt" to LangType.LANG_PT,
            "es" to LangType.LANG_ES,
            "ar" to LangType.LANG_AR,
            "de" to LangType.LANG_DE,
            "it" to LangType.LANG_IT,
            "hi" to LangType.LANG_HI, // 印度语
            "ru" to LangType.LANG_RU, // 俄语
        )

        @Volatile
        private var instance: YiDunCaptchaUtil? = null

        @JvmStatic
        fun getInstance() = instance ?: synchronized(YiDunCaptchaUtil::class.java) {
            instance ?: YiDunCaptchaUtil().also {
                instance = it
            }
        }
    }

    fun verify(activity: Activity, listener: (String?, Error?) -> Unit) {
        val builder = CaptchaConfiguration.Builder()
        val appLanguageCode =
            SpUtils.getInstance().getString(CommonConstants.Key.KEY_APP_LANGUAGE_CODE)
        val configuration = builder.captchaId(CAPTCHA_ID)
            .languageType(yidunLocaleMapping[appLanguageCode] ?: LangType.LANG_EN_US)
            .listener(object : CaptchaListener {
                override fun onValidate(
                    result: String?,
                    validate: String?,
                    msg: String?,
                    captchaType: String?,
                    errorValidate: String?
                ) {
                    LogUtils.d(TAG, "result: $result")
                    LogUtils.d(TAG, "validate: $validate")
                    LogUtils.d(TAG, "msg: $msg")
                    LogUtils.d(TAG, "captchaType: $captchaType")
                    listener.invoke(validate, null)
                }

                override fun onError(code: Int, msg: String?) {
                    LogUtils.e(TAG, "onError code:${code} msg : $msg")
                    listener.invoke(null, Error("code:${code} msg : $msg"))
                }

                override fun onClose(closeType: Captcha.CloseType?) {
                    if (closeType == Captcha.CloseType.USER_CLOSE) {
                        listener.invoke(RecaptchaUtil.RECAPTCHA_CANCEL, null)
                    }
                }

                override fun onCaptchaShow() {
                }
            })
            .build(activity)
        val captcha = Captcha.getInstance()
        captcha.init(configuration)
        captcha.validate()
    }

    fun destroy() {
        Captcha.getInstance().destroy()
    }
}